<template>
  <div class="contentbox">
    <el-card class="box-card">
      <el-form
        ref="form"
        label-width="60px"
        v-show="isShow"
        inline
        label-position="left"
      >
        <el-form-item label="用户名">
          <el-input
            style="width: 100%; margin-right: 10px"
            placeholder="请输入"
            v-model="iptUsername"
          />
        </el-form-item>
        <el-form-item label="手机">
          <el-input
            style="width: 100%; margin-right: 10px"
            placeholder="请输入"
            v-model="iptPhone"
          />
        </el-form-item>
        <el-form-item label="心别" label-width="70px">
          <el-select v-model="iptSex" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            plain
            size="small"
            icon="el-icon-search"
            @click="searchRole"
            >查询</el-button
          >
          <el-button
            plain
            size="small"
            icon="el-icon-refresh"
            @click="iptUsername = iptPhone = iptSex = ''"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <div style="display: flex; justify-content: space-between">
        <span>
          <el-button type="primary" plain size="small" icon="el-icon-plus"
            >新增</el-button
          >
          <el-button type="warning" plain size="small" icon="el-icon-download"
            >导出</el-button
          >
        </span>
        <span>
          <el-button
            icon="el-icon-arrow-up"
            circle
            @click="isShow = !isShow"
          ></el-button>
          <el-button
            icon="el-icon-refresh"
            circle
            @click="refrshData"
          ></el-button>
          <el-button icon="el-icon-menu" circle></el-button>
        </span>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%; margin: 20px 0"
        row-key="id"
        border
        :tree-props="{ children: 'children' }"
      >
        <el-table-column
          prop="title"
          label="#"
          sortable
          width="40"
          type="index"
        />
        <el-table-column prop="id" label="用户ID" sortable width="180">
        </el-table-column>
        <el-table-column prop="username" label="用户名" sortable width="180">
        </el-table-column>
        <el-table-column prop="mobile" label="手机号" sortable width="240">
        </el-table-column>
        <el-table-column
          prop="icon"
          label="用户头像"
          align="center"
          width="200"
        >
          <template #default="{ row }">
            <img
              :src="`http://124.223.14.236:8060/${row.icon}`"
              alt=""
              style="width: 40px; height: 40px"
            />
          </template>
        </el-table-column>
        <el-table-column prop="sex" label="性别" width="210" align="center">
          <template #default="{ row }">
            <el-button
              style="margin: 4px 10px 4px 0"
              type="success"
              plain
              size="mini"
              >{{ row.sex ? '女' : '男' }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column prop="birthday" label="出生年月"> </el-table-column>
        <el-table-column
          prop="component"
          label="操作"
          width="300"
          align="center"
        >
          <template #default="{ row }">
            <el-button type="primary" icon="el-icon-edit" circle></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="delMenu(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="text-align: right"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getUserListDataAPI } from '@/api/vipMange/user'
export default {
  name: 'role',
  data () {
    return {
      options: [
        {
          value: '1',
          label: '女'
        },
        {
          value: '0',
          label: '男'
        }
      ],
      value: '',
      isShow: true,
      iptUsername: '',
      iptPhone: '',
      iptSex: '',
      tableData: [],
      pageData: {
        sex: '',
        username: '',
        mobile: '',
        page: 1,
        limit: 5
      },
      total: 0
    }
  },
  methods: {
    /**
     * 数据初始化
     */
    refrshData () {
      this.pageData = {
        page: 1,
        limit: 5,
        name: ''
      }
      this.getPageRole()
    },
    // 搜索获取查询数据
    async searchRole () {
      if (!this.iptUsername && !this.iptPhone && !this.iptSex) {
        this.$message.warning('至少输入一项查询内容！')
        return
      } else {
        this.pageData.username = this.iptUsername
        this.pageData.phone = this.iptPhone
        this.pageData.sex = this.iptSex
        this.getPageRole()
        this.$message.success('搜索成功')
      }
    },
    /**
     * 当前页变化
     */
    handleCurrentChange (val) {
      this.pageData.page = val
      this.getPageRole()
    },
    /**
     * 最大显示条目发生变化
     */
    handleSizeChange (val) {
      this.pageData.limit = val
      this.getPageRole()
    },
    /**
     * 删除菜单项
     */
    delMenu (id) {
      this.$confirm('您确定需要删除此菜单项嘛？', '提示')
        .then(async () => {
          await delUserAPI(id)
          this.$message.success('删除成功！')
          this.getAllMenu()
        })
        .catch(() => {
          console.log('我也不确定')
        })
    },

    /**
     * 获取分页数据
     */
    async getPageRole () {
      const { data } = await getUserListDataAPI(this.pageData)
      console.log('用户列表数据:', data)
      this.tableData = data.data
      this.total = data.count
    }
  },
  created () {
    this.getPageRole()
  }
}
</script>

<style scoped lang="scss">
.contentbox {
  margin: 10px 20px;
}
::v-deep .el-input__inner {
  height: 33px;
  line-height: 33px;
}
</style>
